// pages/confirm/confirm.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    display:"none",
    _num: 1, //配送方式 1上门  2自提
    order_no:'',
    coupon: '优惠券',
    array: [],
    useid:-1,
    rule:'',
    peisongfangshi: '', //配送方式 1上门  2自提
    distribution_pay: 1, //0-->自提 1-->配送,
  },
  //优惠券
  bindPickerChange: function (e) {
    let that = this;
    var money = that.data.money;    //价格    Total
    var Total = that.data.Total;     //合计价格
    //使用优惠券
    if (that.data.array[e.detail.value].useid != -1){
      console.log('使用优惠券')
      if (money >= that.data.array[e.detail.value].full_money) {
        Total = money - that.data.array[e.detail.value].money
        that.setData({
          index: e.detail.value,
          coupon: that.data.array[e.detail.value].name,
          useid: that.data.array[e.detail.value].useid,
          Total: Total
        })
      } else {      //优惠券未达到额度
        wx.showToast({
          title: '优惠券未达到使用额度',
          icon:'none'
        })
        Total = money
        that.setData({
          coupon: '未使用优惠券',
          useid: -1,
          Total: money
        })
      }
    }else{
      that.setData({
        coupon: '未使用优惠券',
        useid: -1,
        Total: money
      })
      console.log('未用优惠券')
    }
    
    // console.log(Total)
    // console.log('picker发送选择改变，携带值为', e.detail.value)


  },
  liuyan:function(e){
      this.setData({
        liuyan:e.detail.value
      })
  },
  show_payment:function(){
    let that=this;
    wx.request({
      url: app.data.Url +'order/insertOrder',
      data:{
        goods_data:{
          addressid: that.data.add.uaid,
          attrid: that.data.attrid,
          goodsid:that.data.goodsid,
          number:that.data.num,
          remark: that.data.liuyan,
          rule:that.data.rule,
          useid:that.data.useid,
          attrid: that.data.attrid
          
        },
        uid: wx.getStorageSync('userInfo').uid,
        distribution_pay: that.data.distribution_pay
      },
      method:'post',
      success:function(res){
        console.log(res,'success')
     
        that.setData({
          order_no: res.data.data.order_no
        })
      }
    })
    // wx.request({
    //   url: '',
    //   data:{

    //   },
    //   method:'get',
    //   success:function(res){

    //   }
    // })
    this.setData({
      display:"block"
    })

  },
  hideview: function () {
    let  that=this;
    wx.request({
      url: app.data.Url+'order/orderPayParams',
      data:{
        order_no:that.data.order_no,
        title:'智慧社区订单支付',
        uid:wx.getStorageSync('userInfo').uid
      },
      method:'post',
      success:function(res){
        console.log(res)
        wx.requestPayment({
          'timeStamp': res.data.data.weixin.timeStamp,
          'nonceStr': res.data.data.weixin.nonceStr,
          'package': res.data.data.weixin.package,
          'signType': 'MD5',
          'paySign': res.data.data.weixin.paySign,
          'success': function (res) {
            wx.navigateTo({
              url: '../order/order?num=2',
            })

            },
          'fail': function (res) {
              wx.navigateTo({
                url: '../order/order?num=1',
              })
            }
        })
      }
    })
    this.setData({
      display: "none"
    })
  },
  focus_click: function (e) {
    console.log(e.target.dataset.num);
    if (e.target.dataset.num==2){
      //自提
      this.setData({
        distribution_pay:0
      })
    }else{
      this.setData({
        distribution_pay: 1
      })
    }
    this.setData({
      _num: e.target.dataset.num,
      peisongfangshi: e.target.dataset.num
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this;
    console.log(options.guige, 'options.guige')
    wx.request({
      url: app.data.Url +'goods/detail',
      data:{
        goodsid:options.goodsid
      },
      success:function(res){
        that.setData({
          detail: res.data.data,
          num:options.num,
          money: (parseFloat(options.price*100) * parseFloat(options.num) )/100,
          Total: (parseFloat(options.price * 100) * parseFloat(options.num)) / 100,
          price: options.price ,
          goodsid:options.goodsid,
          rule:options.guige,
          pic: res.data.data.pic,
          attrid: res.data.data.specification[options.attrid].attrid
        })
     
      }
    })
    //优惠券
    //https://zhapp.4hl.cn/app/Coupon/usercoupon?uid=XXX&youtype=XXX
    wx.request({
      url: 'https://zhapp.4hl.cn/app/Coupon/usercoupon',
      data: {
        uid: wx.getStorageSync('userInfo').uid,
        youtype: 1
      },
      method: 'get',
      success: function (res) {
        console.log(res.data)
        that.setData({
          array: res.data.data
        })

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    //获取缓存  判断 选择的地址
    wx.getStorage({
      key: 'address',
      success: function (res) {
        console.log(res)
        that.setData({
          add: res.data
        })
        wx.removeStorage({
          key: 'address',
          success: function (res) {
            console.log(res.data)
          }
        })
      },
      fail: function () {
        wx.request({
          url: app.data.Url + 'address/get_defaultadd',
          data: {
            uid: wx.getStorageSync('userInfo').uid,
          },
          method: 'get',
          success: function (res) {
            console.log(res, '-----')
            if (!res.data.data.city) {
              wx.showModal({
                title: '提示',
                content: '请添加默认地址',
                showCancel: false,
                success: function () {
                  wx.navigateTo({
                    url: '/pages/address/address',
                  })
                }
              })
            }else{
              that.setData({
                add: res.data.data
              })
            }
          }
        })
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})